package com.xuan.juc.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/*
*
*
* */
public class completableFutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
// 无返回值的异步回调
        // 发起一个请求
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//
//            try {
//                TimeUnit.SECONDS.sleep(2);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("222");
//
//        });
//
//        System.out.println("111");
//        voidCompletableFuture.get(); // 获取阻塞执行结果


        // 有返回值的异步回调

        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("111");
            return 111;
        });

        integerCompletableFuture.whenComplete((t,u) -> {
            System.out.println(t); // 正常的返回结果
            System.out.println(u); // 错误信息
        }).exceptionally((e)->{
            System.out.println(e.getMessage());
            return 222;
                }
        );
        integerCompletableFuture.get();
    }
}
